package com.camemax.rabbitmq.utils;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

import java.util.Map;

public class QueueModulesRunnable implements Queues, Runnable {

    private ConnectionFactory connectionFactory = RabbitMQUtils.createFactory();
    private Connection connection = null;
    private Channel channel = null;
    private String queueName;
    private boolean durable;
    private boolean exclusive;
    private boolean autoDelete;
    private Map<String,Object> arguments;

    public QueueModulesRunnable(String queueName, boolean durable, boolean exclusive, boolean autoDelete, Map<String, Object> arguments) {
        this.queueName = queueName;
        this.durable = durable;
        this.exclusive = exclusive;
        this.autoDelete = autoDelete;
        this.arguments = arguments;
    }

    @Override
    public void run() {
        try{
            connection = connectionFactory.newConnection();
            channel = connection.createChannel();
            channel.queueDeclare(
                    queueName,
                    durable,
                    exclusive,
                    autoDelete,
                    arguments
            );
            System.out.println("Queue ["+ queueName +"] created successfully!");
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            RabbitMQUtils.close(channel, connection);
        }
    }
}
